/*
 * @Descripttion:
 * @version:
 * @Author: ChenXiang
 * @Date: 2021-07-24 23:02:23
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-08-24 16:26:29
 */

import React from "react";
import FieldContext from "./FieldContext";
import useForm from "./useForm";

export default function Form({ form, children, onFinish, onFinishFailed }) {
  const [formInstance] = useForm(form);
  // 将onFinish的自定义方法传入useForm
  formInstance.setCallback({
    onFinish,
    onFinishFailed
  });
  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        formInstance.submit();
      }}
    >
      {/* 将form实例放入context */}
      <FieldContext.Provider value={formInstance}>
        {children}
      </FieldContext.Provider>
    </form>
  );
}
